package com.swm.yzgrtk;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.swm.yzgrtk.utils.CRC24util.hexStringToByteArray;

/**
 * @author 师文明
 * @data 2025/2/5 下午4:35
 **/
public class test {
    public static void main(String[] args) {
        /*String bcc = yiHuo("81C60100");
        System.out.println(bcc);*/
        String gga = "$BDGGA,075707.000,3448.225849,N,11331.786988,E,1,05,5.63,114.976,M,-19.766,M ,,*5E";
        /*ZonedDateTime parse = ZonedDateTime.parse("$BDGGA,075707.000,3448.225849,N,11331.786988,E,1,05,5.63,114.976,M,-19.766,M ,,*5E");
        System.out.println(parse);
*/
        /*Map<String, String> stringStringMap = parseGGA(gga);
        System.out.println(stringStringMap);*/
        /*reslovedata("$BDRMC,004947.000,A,3448.227373,N,11331.811705,E,0.013,296.88,050325,,E,A,V*70\n" +
                "$BDGGA,004947.000,3448.227373,N,11331.811705,E,1,06,1.46,56.208,M,-19.766,M,,*6D\n" +
                "$BDGSA,A,3,09,10,19,20,43,60,,,,,,,2.65,1.46,2.20,4*0A\n" +
                "$BDGSV,7,1,25,01,36,134,00,02,38,225,30,03,48,185,00,04,24,120,00,1*7B\n" +
                "$BDGSV,7,2,25,05,20,248,00,06,73,011,00,07,54,194,00,09,63,333,40,1*7D\n" +
                "$BDGSV,7,3,25,10,43,203,29,16,72,037,00,19,30,291,43,20,35,231,41,1*7B\n" +
                "$BDGSV,7,4,25,23,13,080,00,27,27,152,00,28,37,099,00,32,07,182,00,1*7F\n" +
                "$BDGSV,7,5,25,37,56,044,00,39,71,065,00,40,58,174,00,43,09,040,30,1*7A\n" +
                "$BDGSV,7,6,25,46,45,301,00,48,37,259,00,59,39,140,00,60,34,228");*/
        byte[] bytes = hexStringToByteArray("\r\n");
        System.out.println(bytes);
    }

    public static void reslovedata(String data) {
        if (data.contains("GSV,")) {
            TreeMap<Integer, String> map = new TreeMap<>();
            String[] split1 = data.split("GSV,");
            for (int i = 1; i < split1.length; i++) {
                String[] split = split1[i].split("\n");
                String[] split2 = split[0].split(",");
                int i1 = split2.length - 4;
                if (i1 % 4 == 0) {
                    int i2 = i1 / 4;
                    for (int j = 0; j < i2; j++) {
                        map.put(Integer.parseInt(split2[j * 4 + 3]), split2[j * 4 + 6]);
                    }
                }

                System.out.println(split2);
                System.out.println(map);
            }
            System.out.println(map);
            System.out.println(44);
        }
    }


    public static Map<String, String> parseGGA(String sentence) {
        Map<String, String> data = new HashMap<>();
        String regex = "\\$BD(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*)\\*";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(sentence);

        if (matcher.find()) {
            data.put("Time", matcher.group(2)); // UTC时间
            data.put("Latitude", matcher.group(3)); // 纬度度分格式
            data.put("LatitudeDirection", matcher.group(4)); // 纬度方向
            data.put("Longitude", matcher.group(5)); // 经度度分格式
            data.put("LongitudeDirection", matcher.group(6)); // 经度方向
            data.put("GPSQuality", matcher.group(7)); // GPS质量指示符
            data.put("SatellitesInUse", matcher.group(8)); // 使用的卫星数量
            data.put("HorizontalDilutionOfPrecision", matcher.group(9)); // 水平精度因子
            data.put("Altitude", matcher.group(10)); // 海拔高度（米）
            data.put("AltitudeUnits", matcher.group(11)); // 海拔单位（米）
            data.put("GeoidalSeparation", matcher.group(12)); // 地理分離（米）
            data.put("GeoidalSeparationUnits", matcher.group(13)); // 地理分離单位（米）
            // 注意，最后一个字段是校验和，通常不需要解析，除非进行校验验证。
        } else {
            throw new IllegalArgumentException("Invalid GGA sentence format.");
        }
        return data;
    }

    /**
     * BCC数据校验 异或处理
     */
    public static String yiHuo(String content) {
       /* int a = 0;
        for (int i = 0; i < content.length() / 2; i++) {
            a = a ^ Integer.parseInt(content.substring(i * 2, (i * 2) + 2), 16);
        }
        String result = Integer.toHexString(a);
        if (result.length() == 1) {
            return "0" + result;
        } else {
            return result;
        }*/
        byte[] bytes = {(byte) 129, (byte) 198, 1, 0};//HexString2Bytes(content);
        HexString2Bytes(content);
        byte b = calculateChecksum(bytes);
        System.out.println(byteToHex(b));
        return byteToHex(b);
    }
    /**
     * byte转十六进制字符
     *
     * @param b byte
     * @return 十六进制字符
     */
    public static String byteToHex(byte b) {
        String hex = Integer.toHexString(b & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }
        return hex.toUpperCase(Locale.getDefault());
    }
    /**
     * 异或校验
     */
    private static byte calculateChecksum(byte[] data) {
        byte checksum = 0;
        for (byte b : data) {
            checksum ^= b;
        }
        return checksum;
    }

    /**
     * 16进制转字节数组
     *
     * @param hexstr 需要转换的Hex字符串
     * @return 转换后的byte数组
     */
    // 向客户端回复数据
    public static byte[] HexString2Bytes(String hexstr) {
        char[] tempChar = hexstr.toCharArray();
        byte[] b = new byte[tempChar.length / 2];
        int j = 0;
        for (int i = 0; i < b.length; i++) {
            char c0 = tempChar[j];
            char c1 = tempChar[j + 1];
            j = j + 2;
            b[i] = (byte) ((parse(c0) << 4) | parse(c1));
        }
        return b;
    }
    public static int parse(char c) {
        if ((c >= '0') && (c <= '9')) {
            return c - 0x30;
        }
        if (c >= 'a')
            return (c - 'a' + 10) & 0x0f;
        if (c >= 'A')
            return (c - 'A' + 10) & 0x0f;
        return (c - '0') & 0x0f;
    }


}
